﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Wedn.Core.Entity;
using Wedn.Core.Service;
using Wedn.Core.Utility;
using Wedn.TemplateEngine.UC;

namespace Wedn.Web.UI
{
    public class PageBase : Page
    {
        /// <summary>
        /// 设置站点Title
        /// </summary>
        /// <param name="title">标题</param>
        protected virtual void SetTitle(string title)
        {
            if (string.IsNullOrEmpty(title))
                return;
            const string tag = "\r\n<title>{0}</title>";
            AddHead(tag, title);
            //Page.Title = title;
        }
        /// <summary>
        /// 设置站点关键字
        /// </summary>
        /// <param name="keywords">关键词</param>
        protected virtual void SetMetaKeywords(string keywords)
        {
            if (string.IsNullOrEmpty(keywords))
                return;
            const string tag = "\r\n<meta name=\"keywords\" content=\"{0}\" />";
            AddHead(tag, keywords);
            //Page.MetaKeywords = keywords;
        }
        /// <summary>
        /// 设置站点描述
        /// </summary>
        /// <param name="description">描述</param>
        protected virtual void SetMetaDescription(string description)
        {
            if (string.IsNullOrEmpty(description))
                return;
            const string tag = "\r\n<meta name=\"description\" content=\"{0}\" />";
            AddHead(tag, description);
        }
        /// <summary>
        /// 在响应流追加默认类型
        /// </summary>
        protected virtual void AddDefaultLanguages()
        {
            Response.AppendHeader("Content-Style-Type", "text/css");
            Response.AppendHeader("Content-Script-Type", "text/javascript");
        }
        /// <summary>
        /// 响应头添加元数据ContentType
        /// </summary>
        protected virtual void AddMetaContentType()
        {
            var meta = new HtmlMeta
            {
                HttpEquiv = "Content-Type",
                Content = string.Format("{0}; charset={1}", Response.ContentType, Response.ContentEncoding.HeaderName)
            };
            //Page.Header.Controls.AddAt(0, meta);
            HtmlTextWriter htw = new HtmlTextWriter(Response.Output);
            meta.RenderControl(htw);
        }
        /// <summary>
        /// 响应流追加一对元数据标签
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">值</param>
        protected virtual void AddMetaTag(string name, string value)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
                return;
            const string tag = "\r\n<meta name=\"{0}\" content=\"{1}\" />";
            //Page.Header.Controls.AddAt(0, new LiteralControl(string.Format(tag, name, value)));
            AddHead(tag, name, value);
        }
        /// <summary>
        /// 响应流追加外链CSS样式表
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        protected virtual void AddLinkCSS(string relativePath)
        {
            if (string.IsNullOrEmpty(relativePath))
                return;
            const string tag = "\r\n<link type=\"text/css\" href=\"{0}\" rel=\"stylesheet\" />";
            AddHead(tag, relativePath);
        }
        /// <summary>
        /// 响应流追加外链JavaScript执行脚本
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        protected virtual void AddScript(string relativePath)
        {
            if (string.IsNullOrEmpty(relativePath))
                return;
            const string tag = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
            AddHead(tag, relativePath);
        }
        /// <summary>
        /// 在head节点中增加数据
        /// </summary>
        /// <param name="format">格式字符串</param>
        /// <param name="value">值</param>
        private void AddHead(string format, params string[] values)
        {
            HtmlTextWriter htw = new HtmlTextWriter(Response.Output);
            new LiteralControl(string.Format(format, values)).RenderControl(htw);
        }
    }
}
